package com.iyue.sys.service;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.iyue.SpringHelper;
import com.iyue.common.service.AbstractGenericServiceImpl;
import com.iyue.framework.cache.IMemcachedCache;
import com.iyue.framework.util.GsonUtil;
import com.iyue.sys.pojo.SysCity;
import com.iyue.sys.pojo.SysNickname;
import com.iyue.sys.pojo.SysPhoneArea;
import com.iyue.sys.pojo.SysProvince;
import com.iyue.sys.pojo.SysUrl;
import com.iyue.sys.pojo.rowmapper.SysCityRowMapper;
import com.iyue.sys.pojo.rowmapper.SysPhoneAreaRowMapper;
import com.iyue.sys.pojo.rowmapper.SysProvinceRowMapper;

public class SysBiz extends AbstractGenericServiceImpl {

	/**
	 * 手机号段
	 */
	private static final String SYS_PHONE_AREA = "com.iyue.sys.pojo.sys_phone_area_";
	/**
	 * 内容中的超链接URL限制
	 */
	private static final String SYS_URL = "com.iyue.sys.pojo.sys_url";
	/**
	 * 省列表
	 */
	private static final String SYS_PROVINCES = "com.iyue.sys.pojo.sys_province_list_";
	/**
	 * 根据省ID获取城市信息
	 */
	private static final String SYS_CITYS_PID = "com.iyue.sys.pojo.sys_city_pid_";
	/**
	 * 根据省ID获取省信息
	 */
	private static final String SYS_PROVINCE = "com.iyue.sys.pojo.sys_province_";
	/**
	 * 根据城市ID获取市信息
	 */
	private static final String SYS_CITY = "com.iyue.sys.pojo.sys_city_";

	private static IMemcachedCache cache = (IMemcachedCache) SpringHelper
			.getBean("FastMemcachedCache");

	/**
	 * 根据手机号段获取所属省市信息
	 *
	 * @param mobile
	 *            手机号段（前七位）
	 * @return
	 */
	public SysPhoneArea phoneAea(String mobile) {
		if (StringUtils.isEmpty(mobile)) {
			return null;
		}

		mobile = mobile.substring(0, 7);

		Gson gson = GsonUtil.getGson();
		String key = SYS_PHONE_AREA + mobile;
		Object obj = cache.get(key);
		if (obj != null) {
			if (obj instanceof String) {
				String json = (String) obj;
				return gson.fromJson(json, SysPhoneArea.class);
			} else {
				cache.delete(key);
			}
		}

		SysPhoneArea ret = getPhoneArea(mobile);

		cache.add(key, gson.toJson(ret), IMemcachedCache.MONTH);

		return ret;
	}

	public SysPhoneArea getPhoneArea(String mobile) {
		String sql = " select prov_code,area_code,province,city from "
				+ SysPhoneArea.table() + " where mp_head = ?";
		// SysPhoneAreaJdbcDAO dao = (SysPhoneAreaJdbcDAO) DAOHelper.getDAO(
		// "SysPhoneAreaJdbcDAO", true);
		SysPhoneArea ret = this.getSysdao_r().get(sql, new Object[] { mobile },
				new SysPhoneAreaRowMapper());
		if (ret == null) {
			return null;
		}

		return ret;
	}

	/**
	 * 获取省列表
	 */
	public SysProvince getProvinceById(String id) {
		Gson gson = GsonUtil.getGson();
		String key = SYS_PROVINCE + id;
		Object obj = cache.get(key);
		if (obj != null) {
			if (obj instanceof String) {
				String json = (String) obj;
				return gson.fromJson(json, SysProvince.class);
			} else {
				cache.delete(key);
			}
		}

		String sql = " select id, name from " + SysProvince.table()
				+ " where id = ?";
		// SysPhoneAreaJdbcDAO dao = (SysPhoneAreaJdbcDAO) DAOHelper.getDAO(
		// "SysPhoneAreaJdbcDAO", true);
		SysProvince ret = this.getSysdao_r().get(sql, new Object[] { id },
				new SysProvinceRowMapper());
		if (ret == null) {
			cache.add(key, ret, IMemcachedCache.MINUTE);

			return null;
		}

		cache.add(key, gson.toJson(ret), IMemcachedCache.MONTH);

		return ret;
	}

	/**
	 * 获取省列表
	 */
	public List<SysProvince> getProvince() {
		Gson gson = GsonUtil.getGson();
		String key = SYS_PROVINCES;
		Object obj = cache.get(key);
		if (obj != null) {
			if (obj instanceof String) {
				String json = (String) obj;
				Type listType = new TypeToken<List<SysProvince>>() {
				}.getType();
				return gson.fromJson(json, listType);
			} else {
				cache.delete(key);
			}
		}

		String sql = " select id, name from " + SysProvince.table();
		// SysPhoneAreaJdbcDAO dao = (SysPhoneAreaJdbcDAO) DAOHelper.getDAO(
		// "SysPhoneAreaJdbcDAO", true);
		List<SysProvince> ret = this.getSysdao_r().query(sql, new Object[] {},
				new SysProvinceRowMapper());
		if (ret == null || ret.isEmpty()) {
			cache.add(key, ret, IMemcachedCache.MINUTE);

			return null;
		}

		cache.add(key, gson.toJson(ret), IMemcachedCache.MONTH);

		return ret;
	}

	/**
	 * 通过省ID获取城市信息
	 */
	public SysCity getCityById(String cid) {
		Gson gson = GsonUtil.getGson();
		String key = SYS_CITY + cid;
		Object obj = cache.get(key);
		if (obj != null) {
			if (obj instanceof String) {
				String json = (String) obj;
				return gson.fromJson(json, SysCity.class);
			} else {
				cache.delete(key);
			}
		}

		String sql = " select id, pid, name from " + SysCity.table()
				+ " where id = ?";
		// SysPhoneAreaJdbcDAO dao = (SysPhoneAreaJdbcDAO) DAOHelper.getDAO(
		// "SysPhoneAreaJdbcDAO", true);
		SysCity ret = this.getSysdao_r().get(sql, new Object[] { cid },
				new SysCityRowMapper());
		if (ret == null) {
			return null;
		}

		cache.add(key, gson.toJson(ret), IMemcachedCache.MONTH);

		return ret;
	}

	/**
	 * 通过省ID获取城市信息
	 */
	public List<SysCity> getCityByPid(String pid) {
		Gson gson = GsonUtil.getGson();
		String key = SYS_CITYS_PID + pid;
		Object obj = cache.get(key);
		if (obj != null) {
			if (obj instanceof String) {
				String json = (String) obj;
				Type listType = new TypeToken<List<SysCity>>() {
				}.getType();
				return gson.fromJson(json, listType);
			} else {
				cache.delete(key);
			}
		}

		String sql = " select id, pid, name from " + SysCity.table()
				+ " where pid = ?";
		// SysPhoneAreaJdbcDAO dao = (SysPhoneAreaJdbcDAO) DAOHelper.getDAO(
		// "SysPhoneAreaJdbcDAO", true);
		List<SysCity> ret = this.getSysdao_r().query(sql, new Object[] { pid },
				new SysCityRowMapper());
		if (ret == null || ret.isEmpty()) {
			cache.add(key, ret, IMemcachedCache.MINUTE);

			return null;
		}

		cache.add(key, gson.toJson(ret), IMemcachedCache.MONTH);

		return ret;
	}
	/**
	 * 通过省ID获取城市信息
	 */
	public List<SysCity> getCitys() {
		Gson gson = GsonUtil.getGson();
		String key = SYS_CITYS_PID + "citys";
		Object obj = cache.get(key);
		if (obj != null) {
			if (obj instanceof String) {
				String json = (String) obj;
				Type listType = new TypeToken<List<SysCity>>() {
				}.getType();
				return gson.fromJson(json, listType);
			} else {
				cache.delete(key);
			}
		}

		String sql = " select id, pid, name from " + SysCity.table()
				+ " order by pid";
		List<SysCity> ret = this.getSysdao_r().query(sql, new Object[] { },
				new SysCityRowMapper());
		if (ret == null || ret.isEmpty()) {
			cache.add(key, ret, IMemcachedCache.MINUTE);
			return null;
		}

		cache.add(key, gson.toJson(ret), IMemcachedCache.MONTH);

		return ret;
	}

	/**
	 * 限制外链
	 *
	 * @return
	 */
	@SuppressWarnings( { "unchecked" })
	public List<String> url() {
		String key = SYS_URL;
		Object obj = cache.get(key);
		if (obj != null) {
			if (obj instanceof List) {
				return (List<String>) obj;
			} else {
				cache.delete(key);
			}
		}

		String sql = " select url from " + SysUrl.table() + " where status = 1";
		// SysUrlJdbcDAO dao = (SysUrlJdbcDAO) DAOHelper.getDAO("SysUrlJdbcDAO",
		// true);
		List list = this.getSysdao_r().query(sql);
		if (list == null || list.isEmpty()) {
			return null;
		}

		List<String> ret = new ArrayList<String>();
		for (int i = 0; i < list.size(); i++) {
			Map<String, String> map = (Map<String, String>) list.get(i);
			if (map == null || map.isEmpty()) {
				return null;
			}

			String url = map.get("url");
			if (url == null || url.length() < 3) {
				continue;
			}

			ret.add(url);
		}

		if (ret == null || ret.isEmpty()) {
			return null;
		}

		cache.add(key, ret, IMemcachedCache.DAY);

		return ret;
	}

	public boolean getSysNickname(String nickname) {
		String sql = " select count(nickname) FROM " + SysNickname.table()
				+ " WHERE nickname = ?";
		// SysUrlJdbcDAO dao = (SysUrlJdbcDAO) DAOHelper.getDAO("SysUrlJdbcDAO",
		// true);
		int row = this.getSysdao_r().getRowsCount(sql,
				new Object[] { nickname });
		if (row == 0) {
			return true;
		}

		return false;
	}
}
